/*
 * Copyright (C) 2009 Jakub Miara
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.araim.rringtone.db;

import java.util.ArrayList;

import com.araim.rringtone.RingtoneInfo;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RandomRingtoneDatabase extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "araim.RandomRingtone";
	private static final int DATABASE_VERSION = 2;

	public RandomRingtoneDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE ringtones (id INTEGER , name TEXT not null ,uri TEXT not null)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("CREATE TABLE ringtones2 (id INTEGER , name TEXT not null ,uri TEXT not null)");
		db.execSQL("INSERT INTO ringtones2 (id,name,uri) SELECT id,name,uri FROM ringtones");
		db.execSQL("DROP TABLE ringtones");
		db.execSQL("ALTER TABLE ringtones2 RENAME TO ringtones");
	}

	public void insertRingtone(RingtoneInfo ri) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.insert("ringtones", "", ri.getContentValues());
	}

	public void insertRingtones(RingtoneInfo[] ris) {
		SQLiteDatabase db = this.getWritableDatabase();
		for(RingtoneInfo ri:ris){
			db.insert("ringtones", "", ri.getContentValues());
		}
	}
	
	public void clear() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DELETE FROM ringtones");
	}

	public void delete(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DELETE FROM ringtones WHERE id = ?", new Object[] { id });
	}

	public RingtoneInfo[] getAllRingtonesArray() {
		return getAllRingtones().toArray(new RingtoneInfo[0]);
	}

	public ArrayList<RingtoneInfo> getAllRingtones() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.close();
		db = this.getReadableDatabase();
		Cursor c = db.query("ringtones", new String[] { "id", "name", "uri" },
				null, null, null, null, null);
		ArrayList<RingtoneInfo> ringtones = new ArrayList<RingtoneInfo>();
		if (c.moveToFirst()) {
			while (!c.isAfterLast()) {
				RingtoneInfo r = new RingtoneInfo();
				r.setId(c.getInt(0));
				r.setName(c.getString(1));
				r.setUri(c.getString(2));
				ringtones.add(r);
				c.moveToNext();
			}
		}
		return ringtones;
	}

}
